/*
 * @Description: 全宗reducer
 * @Author: Rfan
 * @Date: 2022-01-08 16:30:05
 * @LastEditTime: 2022-04-20 15:12:21
 */

import {
  ADD_LEVEL_BY_ID,
  ADD_LEVEL_METADATA,
  ARC_TYPE_LOADING,
  CLEAR_LEVEL_MATEDATA,
  EDIT_LEVEL_LIST,
  METADATA_LOADING,
  METADATA_LOADING_FINISH,
  SET_ARC_TYPE_LIST,
  SET_CURRENT_METADATA,
  SET_DIALOG_STATE,
  SET_FONDS_LIST,
  SET_METADATA_LIST
} from '../constants';

export const ArchivingReducer = (state, action) => {
  switch (action.type) {
    // 表格加载loading
    case ARC_TYPE_LOADING:
      return { ...state, loading: true };

    /**
     * @description: 加载表格数据及相关数据
     * @param {Array} data 表格数据
     * @return {*}
     */
    case SET_FONDS_LIST: {
      return {
        ...state,
        fondsList: action.data
      };
    }

    /**
     * @description: 加载表格数据及相关数据
     * @param {Array} data 表格数据
     * @return {*}
     */
    case SET_ARC_TYPE_LIST: {
      const ATList = action.data.map((item) => {
        item.treeName = item.atName;
        item.key = item.id;
        item.isLeaf = true;
        return item;
      });
      const curFond = state.fondsList.find((fond) => fond.id === action.id);
      return {
        ...state,
        ATList,
        currentFond: curFond
      };
    }

    /**
     * @description: 加载表格数据及相关数据
     * @param {Array} data 表格数据
     * @param {Number} id 档案类型ID
     * @return {*}
     */
    case ADD_LEVEL_BY_ID: {
      return {
        ...state,
        levelList: action.data,
        currentAt: action.id
      };
    }

    /**
     * @description: 编辑元数据列表
     * @param {string} level 表格数据
     */
    case EDIT_LEVEL_LIST: {
      const _levelList = state.levelList.map((item) => {
        if (item.level === parseInt(action.level, 10)) {
          item.upperInfo = action.data;
        }
        return item;
      });
      console.log('edit', _levelList);
      return {
        ...state,
        levelList: _levelList,
        currentLevel: `${action.level}`,
        metadataList: state.levelMatedata[action.level] || []
      };
    }

    // 表格加载loading
    case METADATA_LOADING:
      return { ...state, metadataLoading: true };

    // 表格加载loading 结束
    case METADATA_LOADING_FINISH:
      return { ...state, metadataLoading: false };

    /**
     * @description: 加载元数据列表
     * @param {Array} data 表格数据
     */
    case SET_METADATA_LIST: {
      return {
        ...state,
        metadataList: action.data,
        metadataLoading: false
      };
    }

    case ADD_LEVEL_METADATA: {
      return {
        ...state,
        levelMatedata: {
          ...state.levelMatedata,
          [action.level]: action.data
        }
      };
    }

    case SET_CURRENT_METADATA: {
      console.log('set current', action.level, state.levelMatedata[action.level]);
      return {
        ...state,
        currentLevel: `${action.level}`,
        metadataList: state.levelMatedata[action.level] || []
      };
    }

    case CLEAR_LEVEL_MATEDATA: {
      return {
        ...state,
        levelMatedata: {},
        metadataList: []
      };
    }

    case SET_DIALOG_STATE: {
      return { ...state, arrFormVisible: action.state || false };
    }

    default:
      console.log('untouched', action);
      return state;
  }
};
